package com.guanyong.services;

import com.guanyong.dao.ICommentDaoMapper;
import com.guanyong.dao.ICommentGoodLogDaoMapper;
import com.guanyong.dao.ICommentReportLogDaoMapper;
import com.guanyong.dao.IUserBaseDaoMapper;
import com.guanyong.model.*;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * Created by mxk on 2017/6/4.
 */
@Service
public class CommentService {
    @Autowired
    ICommentDaoMapper iCommentDaoMapper;

    @Autowired
    IUserBaseDaoMapper iUserBaseDaoMapper;

    @Autowired
    ICommentGoodLogDaoMapper iCommentGoodLogDaoMapper;

    @Autowired
    ICommentReportLogDaoMapper iCommentReportLogDaoMapper;

    @Autowired
    FilmServices filmServices;

    //分页列表
    public HashMap<String,Object> GetCommentList(CommentModel commentModel,
                                              Integer pno,
                                              Integer pagesize,
                                              Integer allcount){
        HashMap<String,Object> hashMap=new HashMap();
        if (allcount<0){
            Integer count=iCommentDaoMapper
                    .SelectCommentListCount(commentModel);
            hashMap.put("count",count);
        }
        else {
            hashMap.put("count",allcount);
        }
        List<CommentModel> commentModelList
                =iCommentDaoMapper.SelectCommentList(commentModel,(pno-1)*pagesize,
                pagesize);

        StringBuilder stringBuilder=new StringBuilder();
        for (CommentModel commentModel2:
                commentModelList) {
            if (commentModel2.getfCommentid()!=null &&  stringBuilder.indexOf
                    (commentModel2.getfCommentid())==-1){
                stringBuilder.append(",");
                stringBuilder.append("'");
                stringBuilder.append(commentModel2.getfCommentid());
                stringBuilder.append("'");
            }
            if (commentModel2.getfTocommentid()!=null&& stringBuilder.indexOf
                    (commentModel2.getfTocommentid())==-1){
                stringBuilder.append(",");
                stringBuilder.append("'");
                stringBuilder.append(commentModel2.getfTocommentid());
                stringBuilder.append("'");
            }
        }
        if (stringBuilder.length()>1){
            String useridList=stringBuilder.deleteCharAt(0).toString();
            List<UserBaseModel> userBaseModels=iUserBaseDaoMapper.selectUserInfoByIdList
                    (useridList);
            HashMap<String ,String > userNickHash=new HashMap<String, String>();
            HashMap<String ,String > userPhotoHash=new HashMap<String, String>();
            for (UserBaseModel userBaseModel :userBaseModels){
                if (!userNickHash.containsKey(userBaseModel.getfId())){
                    userNickHash.put(userBaseModel.getfId(),userBaseModel.getfUsername());

                    userPhotoHash.put(userBaseModel.getfId(),userBaseModel.getfPhoto());
                }
            }
            //循环 //赋值昵称
            for (CommentModel commentModel2:
                    commentModelList) {
                commentModel2.setfCommentnick(userNickHash.get(commentModel2.getfCommentid()));
                commentModel2.setfTocommentnick(userNickHash.get(commentModel2.getfTocommentid()));
                commentModel2.setfCommentphoto(userPhotoHash.get(commentModel2.getfCommentid()));
                commentModel2.setfTocommentphoto(userPhotoHash.get(commentModel2.getfTocommentid()));
            }

        }
        hashMap.put("list",commentModelList);
        return hashMap;
    }

    //我的评论（包含电影信息）
    public HashMap<String,Object> GetCommentList1_0(CommentModel commentModel,
                                                 Integer pno,
                                                 Integer pagesize,
                                                 Integer allcount){
        HashMap<String,Object> hashMap=new HashMap();
        if (allcount<0){
            Integer count=iCommentDaoMapper
                    .SelectCommentListCount(commentModel);
            hashMap.put("count",count);
        }
        else {
            hashMap.put("count",allcount);
        }
        List<CommentModel> commentModelList
                =iCommentDaoMapper.SelectCommentList(commentModel,(pno-1)*pagesize,
                pagesize);

        StringBuilder stringBuilder=new StringBuilder();
        StringBuilder filmBuilder=new StringBuilder();
        for (CommentModel commentModel2:
                commentModelList) {
            if (commentModel2.getfCommentid()!=null &&  stringBuilder.indexOf
                    (commentModel2.getfCommentid())==-1){
                stringBuilder.append(",");
                stringBuilder.append("'");
                stringBuilder.append(commentModel2.getfCommentid());
                stringBuilder.append("'");
            }
            if (commentModel2.getfTocommentid()!=null&& stringBuilder.indexOf
                    (commentModel2.getfTocommentid())==-1){
                stringBuilder.append(",");
                stringBuilder.append("'");
                stringBuilder.append(commentModel2.getfTocommentid());
                stringBuilder.append("'");
            }
            if (commentModel2.getfObjectid()!=null&& filmBuilder.indexOf
                    (commentModel2.getfObjectid())==-1){
                filmBuilder.append(",");
                filmBuilder.append("'");
                filmBuilder.append(commentModel2.getfObjectid());
                filmBuilder.append("'");
            }

        }
        if (stringBuilder.length()>1 && filmBuilder.length()>0){
            String useridList=stringBuilder.deleteCharAt(0).toString();
            List<UserBaseModel> userBaseModels=iUserBaseDaoMapper.selectUserInfoByIdList
                    (useridList);
            HashMap<String ,String > userNickHash=new HashMap<String, String>();
            HashMap<String ,String > userPhotoHash=new HashMap<String, String>();
            for (UserBaseModel userBaseModel :userBaseModels){
                if (!userNickHash.containsKey(userBaseModel.getfId())){
                    userNickHash.put(userBaseModel.getfId(),userBaseModel.getfUsername());

                    userPhotoHash.put(userBaseModel.getfId(),userBaseModel.getfPhoto());
                }
            }
            String filmList=filmBuilder.deleteCharAt(0).toString();
            List<FilmModel> filmModels= filmServices.GetFilmModelByIdList(filmList);
            HashMap<String ,FilmModel > filmHash=new HashMap<String, FilmModel>();
            for (FilmModel filmModel :filmModels){
                    filmHash.put(filmModel.getfId(),filmModel);
            }



            //循环 //赋值昵称
            for (CommentModel commentModel2:
                    commentModelList) {
                commentModel2.setfCommentnick(userNickHash.get(commentModel2.getfCommentid()));
                commentModel2.setfTocommentnick(userNickHash.get(commentModel2.getfTocommentid()));
                commentModel2.setfCommentphoto(userPhotoHash.get(commentModel2.getfCommentid()));
                commentModel2.setfTocommentphoto(userPhotoHash.get(commentModel2.getfTocommentid()));
                commentModel2.setFilm(filmHash.get(commentModel2.getfObjectid()));
            }
        }
        hashMap.put("list",commentModelList);
        return hashMap;
    }



    //单个删除
    public  Integer UpdateSate(Integer fid,Integer state){
        CommentModel commentModel=new CommentModel();
        commentModel.setfId(fid);
        commentModel.setfShow(state);
        return  iCommentDaoMapper.update(commentModel);
    }




    public   ResultObject<Integer> AddComment(CommentModel commentModel){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        Integer res=-1;
        if (commentModel.getfClasstype()==1){
            if (commentModel.getfParentid()==0){
                res =filmServices.UpdateCommentCount(commentModel.getfObjectid());
                if (res >-1) {
                    res = iCommentDaoMapper.insert(commentModel);
                    if(res >-1){
                        resultObject.setCode(ResultCode.OperateOk);
                    }
                    else{
                        resultObject.setCode(ResultCode.OperateFail);
                        resultObject.setErrorMsg("添加评论失败");
                    }
                }
                else{
                    resultObject.setCode(ResultCode.OperateFail);
                    resultObject.setErrorMsg("添加评论失败");
                }
            }else{
                res = iCommentDaoMapper.insert(commentModel);
                if(res >-1){
                    resultObject.setCode(ResultCode.OperateOk);
                }
                else{
                    resultObject.setCode(ResultCode.OperateFail);
                    resultObject.setErrorMsg("添加评论失败");
                }
            }
        }
        else{
            //nothing is here
        }

        return resultObject;
    }

    public ResultObject<Integer> AddComentGoodLog(CommentGoodLogModel commentGoodLogModel){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        CommentGoodLogModel commentGoodLogModel1=iCommentGoodLogDaoMapper.GetCommentGoodlgModel
                (commentGoodLogModel.getfUserid(),commentGoodLogModel.getfCommentid());
        if (commentGoodLogModel1!=null){
            resultObject.setCode(ResultCode.FiledRepeat);
            resultObject.setErrorMsg("您已经点赞过了!");
        }
        else{
            int res=  iCommentGoodLogDaoMapper.insert(commentGoodLogModel);
            if (res >-1){
                int res2=iCommentDaoMapper.UpdateGoodCt(commentGoodLogModel.getfCommentid());
                if (res2>-1){
                    resultObject.setCode(ResultCode.OperateOk);
                }
                else{
                    resultObject.setCode(ResultCode.OperateFail);
                }
            }
            else{
                resultObject.setCode(ResultCode.OperateFail);
            }
        }

        return  resultObject;

    }


    public  ResultObject<Integer> AddComentReprotLog(CommentReportLogModel commentReportLogModel){

        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        int res  =iCommentReportLogDaoMapper.insert(commentReportLogModel);

        if (res >-1){
            int res2=iCommentDaoMapper.UpdateReportct(commentReportLogModel.getfCommentid());
            if (res2>-1){
                resultObject.setCode(ResultCode.OperateOk);
            }
            else{
                resultObject.setCode(ResultCode.OperateFail);
            }
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return  resultObject;
    }

    //批量删除
    public  Integer UpdatePraentIdSate(Integer fid,Integer state){
        CommentModel commentModel=new CommentModel();
        commentModel.setfId(fid);
        commentModel.setfShow(state);
        Integer res=  iCommentDaoMapper.update(commentModel);
        if (res >-1){
            res=  iCommentDaoMapper.UpdateParentState(state,fid);
        }
        return  res;
    }
}
